package com.railway.switchs.action.business;

import com.railway.switchs.aspect.OperationLog;
import com.railway.switchs.base.BaseResponse;
import com.railway.switchs.constant.SystemConstant;
import com.railway.switchs.domain.dto.business.BaseDevice;
import com.railway.switchs.domain.query.business.BaseDeviceQuery;
import com.railway.switchs.enums.EnumReturnResult;
import com.railway.switchs.service.business.IBaseDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName BaseInfoAction
 * @Author 魏梓晟
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/baseDevice")
public class BaseDeviceAction {

    @Autowired
    private IBaseDeviceService baseDeviceService;

    /**
     * 进入基础设备页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return "baseDevice/list";
    }

    /**
     * 获取基础设备型号信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public BaseDeviceQuery getList(BaseDeviceQuery query) {
        try {
            query = baseDeviceService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取基础设备型号信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取基础设备型号数据异常");
        }
        return query;
    }


    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String preAdd(){
        return "baseDevice/add";
    }

    /**
     * 新增基础设备
     * @param baseDevice
     * @param result
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="4",actionType ="0" )
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated BaseDevice baseDevice, BindingResult result) {
        BaseResponse response = new BaseResponse();
        try {
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("BaseDevice-add参数有误");
                log.info("BaseDevice-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            baseDeviceService.add(baseDevice);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("新增成功");
        } catch (Exception e) {
            log.error("新增基础信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("新增失败");
        }
        return response;
    }

    /**
     * 修改基础设备
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView preEdit(Long id){
        ModelAndView mav = new ModelAndView();
        try{
            BaseDevice baseDevice = baseDeviceService.get(id);
            mav.addObject("baseDevice",baseDevice);
            mav.setViewName("baseDevice/edit");
        }catch (Exception e){
            log.error("获取基础设备详情失败",e);
            mav.addObject(SystemConstant.ERROR_KEY,"系统异常");
            mav.setViewName("baseDevice/list");
        }
        return mav;
    }

    /**
     * 修改基础设备
     * @param baseDevice
     * @param result
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="4",actionType ="1" )
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public BaseResponse edit(@Validated BaseDevice baseDevice, BindingResult result) {
        BaseResponse response = new BaseResponse();
        try {
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("BaseDevice-edit参数有误");
                log.info("BaseDevice-edit参数有误："+result.getAllErrors().toString());
                return response;
            }
            baseDeviceService.edit(baseDevice);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("修改成功");
        } catch (Exception e) {
            log.error("修改基础信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("修改失败");
        }
        return response;
    }

    /**
     * 删除基础设备
     * @param id
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="4",actionType ="2" )
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public BaseResponse delete(Long id) {
        BaseResponse response = new BaseResponse();
        try {
            baseDeviceService.delete(id);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("删除基础设备成功");
        } catch (Exception e) {
            log.error("删除基础设备异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("删除基础设备失败");
        }
        return response;
    }

    /**
     * 获取设备型号数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllList",method = RequestMethod.POST)
    public BaseDeviceQuery getAllList(BaseDeviceQuery query) {
        try {
            query.setData(baseDeviceService.getList(query));
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取设备型号数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取设备型号异常");
        }
        return query;
    }

}
